package com.superm.controller;

import com.superm.entity.Department;
import com.superm.entity.Emp;
import com.superm.entity.EmpJoPo;
import com.superm.entity.Supplier;
import com.superm.service.impl.SupplierServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/supplier")
public class SupplierContorller {

    @Autowired
    private SupplierServiceImpl supplierService;

    /**
     * 跳转到供应商管理页面
     * @return
     */
    @RequestMapping("/suppliers")
    public ModelAndView supplices(){
        ModelAndView mv = new ModelAndView("supplier/SupplierList");
        return mv;
    }

    /**
     * 模糊查询供应商信息
     * @return
     */
    @RequestMapping("/selectSupplier")
    @ResponseBody  //返回真实数据
    public Map<String,Object> selectSupplier(Supplier supplier, @RequestParam("page") Integer pageNo, @RequestParam("limit")Integer pageSize){

        System.out.println("1da32dsadasa321das3da");
        System.out.println(supplier);
        Map<String,Object> map = new HashMap<String,Object>();
        List<Supplier> supplierList =  supplierService.selectLikeSupplierList(supplier,pageNo,pageSize);
        //获取总条数
        Integer count = supplierService.selectSupplierCount(supplier);

        map.put("code",0);
        map.put("msg","");
        map.put("count",count);
        map.put("data",supplierList);
        return map;
    }

    /**
     * 跳转到供应商添加页面
     * @return
     */
    @RequestMapping("/addSuppliers")
    public ModelAndView addSuppliers(Supplier supplier){
        ModelAndView mv = new ModelAndView("supplier/addSupplier");
        return mv;
    }

    /**
     * 供应商信息添加
     * @return
     */
    @RequestMapping("/addSupplier")
    @ResponseBody
    public Map<String,Object> addSupplier(Supplier supplier){
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("code",0);

        if (supplierService.addSupplier(supplier)>0){
            map.put("success",true);
            map.put("msg","添加成功");
        }else {
            map.put("success",false);
            map.put("msg","添加失败");
        }
        return map;
    }

    /**
     * 跳转到用户修改页面
     * @return
     */
    @RequestMapping("/updateSuppliers")
    public ModelAndView updateSuppliers(Integer supplierId){
        ModelAndView mv = new ModelAndView();
        Supplier supplier1 =   supplierService.selectIdSup(supplierId);
        mv.addObject("supplier",supplier1);
        mv.setViewName("supplier/updateSupplier");
        return mv;
    }

    /**
     * 供应商信息修改
     * @return
     */
    @RequestMapping("/updateSupplier")
    @ResponseBody
    public Map<String,Object> updateSupplier(Supplier supplier){
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("code",0);

        if (supplierService.updateSupplier(supplier)>0){
            map.put("success",true);
            map.put("msg","修改成功");
        }else {
            map.put("success",false);
            map.put("msg","修改失败");
        }
        return map;
    }


    /**
     * 删除用户信息
     * @param supplierId
     * @return
     */
    @RequestMapping("/deleteSupplier")
    @ResponseBody
    public Map<String,Object>deleteUser(@RequestParam("supplierId")Integer supplierId){
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("code",0);
        if (supplierService.deleteSupplier(supplierId)>0){
            map.put("success",true);
            map.put("msg","删除成功");
        }else{
            map.put("success",false);
            map.put("msg","删除失败");
        }
        return  map;
    }
}
